package com.sunny;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicInteger;

public class TimerTest extends TimerTask {

    private Timer timer;
    private String name;
    private String[] timerName;

    private static AtomicInteger cnt = new AtomicInteger(0);

    public TimerTest() {
    }

    public TimerTest(String[] timerName) {
        this.timerName = timerName;
    }

    public TimerTest(String name, Timer timer, String[] timerName) {
//        System.out.println("构造方法：name="+name + " timer="+timer);
        this.name = name;
        this.timer = timer;
        this.timerName = timerName;
    }

    public void go(){
//        System.out.println("开启调用");
        timer.schedule(new TimerTest(timerName[cnt.getAndIncrement()%timerName.length], timer, timerName), 1000);
    }

    @Override
    public void run() {
        System.out.println(name);
        timer.schedule(new TimerTest(timerName[cnt.getAndIncrement()%timerName.length], timer, timerName), 2000);
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }

    public static void main(String[] args) {

        Timer timer = new Timer();
        String[] timerName = {"tt1","tt2","tt3"};
        TimerTest tt = new TimerTest(timerName);
        tt.setTimer(timer);
        tt.go();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("another scheduler");
            }
        }, 2000, 900);

    }
}